//
//  UITextField+Style.m
//  YDGJ
//
//  Created by Luke on 16/6/22.
//  Copyright © 2016年 Galaxy360. All rights reserved.
//

#import "UITextField+Style.h"
#import "UIButton+Block.h"
#import <objc/runtime.h>

static const void *kLeftTextKey   = "leftTextKey";
static const void *kLeftViewWidth = "kLeftViewWidth";

/** 输入框字体大小 */
#define TextFieldFontSize         FontCustomSize(16)

/** 输入框字体颜色 */
#define TextFieldFontColor        Color_BlackFont

/** 输入框占位文字颜色 */
#define TextFieldPlaceholderColor Color_grayFont

/** 输入框边框颜色 */
#define TextFieldborderColor      Color_TextBorderColor

/** 输入框右侧按钮背景色 */
#define TextFieldRightBgColor     Color_Main

/** 输入框右侧按钮不可点击色 */
#define TextFieldRightDisableColor  Color_grayFont

@implementation UITextField (Style)

@dynamic leftText, leftImage, PlaceholderText, rightViewEnable;


+ (void)load
{
    Method otherMehtod = class_getInstanceMethod([self class], @selector(gj_setFont:));
    Method originMehtod = class_getInstanceMethod([self class], @selector(setFont:));
    method_exchangeImplementations(otherMehtod, originMehtod);
}

- (void)gj_setFont:(UIFont *)font
{
    if (!font) return;
    
    [self gj_setFont:font];
    
    self.tintColor = Color_Main;
    
    /* 在iPhone4s上,清除按钮黄色高亮效果无效,可用下面这个方法
         #import "FD_Method.h"
        if([FD_Method getVariableWithClass:[UITextField class] varName:@"_clearButton"])
        {
            UIButton *cleanbutton = [self valueForKey:@"_clearButton"];
            [cleanbutton setImage:ImageNamed(@"login_icon") forState:UIControlStateHighlighted];
        }
     */
    
//    //2.过滤掉所有文本输入Emoji表情
        //#import "ReactiveCocoa.h"
        //#import "FD_Method.h"
//    @weakify(self);
//    [self.rac_textSignal subscribeNext:^(id x) {
//        @strongify(self);
//        NSLog(@"输入的字符===%@",x);
//        if (((NSString *)x).length>0) {
//            self.text = [FD_Method disableEmoji:x];
//        }
//    }];
}


/**
 *  设置统一外观风格
 */
- (void)addBorderStyle
{
    self.font = TextFieldFontSize;
    self.layer.borderColor = TextFieldborderColor.CGColor;
    self.returnKeyType = UIReturnKeyNext;
    self.layer.borderWidth = 0.5;
    self.layer.cornerRadius = 3;
    self.layer.masksToBounds = YES;
}

/**
 *  设置左边文字
 */
- (void)setLeftText:(NSString *)leftText
{
    objc_setAssociatedObject(self, kLeftTextKey, leftText, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    if (!leftText) return;
    
    UILabel *leftLab = [[UILabel alloc] init];
    leftLab.text = leftText;
    leftLab.font = TextFieldFontSize;
    leftLab.textColor = TextFieldFontColor;
    
    CGSize size = [leftText sizeWithFont:TextFieldFontSize constrainedToHeight:self.height];
    leftLab.frame = CGRectMake(0, 0, self.leftviewWidth?self.leftviewWidth.floatValue:size.width, self.height);
    
    self.leftView = leftLab;
    self.leftViewMode = UITextFieldViewModeAlways;
}

- (NSString *)leftText
{
    return objc_getAssociatedObject(self, kLeftTextKey);
}

- (NSNumber *)leftviewWidth
{
    return objc_getAssociatedObject(self, kLeftViewWidth);
}

- (void)setLeftviewWidth:(NSNumber *)leftviewWidth
{
    objc_setAssociatedObject(self, kLeftViewWidth, leftviewWidth, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

/**
 *  设置左边图片
 */
- (void)setLeftImage:(UIImage *)leftImage
{
    if (!leftImage) return;
    
    UIImageView *leftView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, self.height)];
    leftView.contentMode = UIViewContentModeCenter;
    leftView.image = leftImage;
    self.leftView = leftView;
    self.leftViewMode = UITextFieldViewModeAlways;
}

/**
 *  设置统一占位文字
 */
- (void)setPlaceholderText:(NSString *)PlaceholderText
{
    if (!PlaceholderText) return;
    
    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:PlaceholderText attributes:@{NSForegroundColorAttributeName: UIColorFromRGB(0xc1c1c1), NSFontAttributeName: TextFieldFontSize}];
}

/**
 *  设置右侧按钮是否可点击
 */
- (void)setRightViewEnable:(BOOL)rightViewEnable
{
    if (self.rightView && [self.rightView isKindOfClass:[UIButton class]]) {
        UIButton *rightBtn = (UIButton *)self.rightView;
        rightBtn.enabled = rightViewEnable;
    }
}

/**
 *  添加右侧按钮,返回点击回调
 */
- (UIButton *)setRightViewTitle:(NSString *)title touchBlock:(void(^)(UIButton *))block
{
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 172/2, self.height)];
    rightBtn.titleLabel.font = TextFieldFontSize;
    [rightBtn setTitle:title forState:0];
    [rightBtn setTitle:title forState:UIControlStateDisabled];
    [rightBtn setTitleColor:WhiteColor forState:0];
    rightBtn.layer.cornerRadius = 3;
    rightBtn.layer.masksToBounds = YES;
    [rightBtn setTitleColor:TextFieldRightDisableColor forState:UIControlStateDisabled];
    [rightBtn setBackgroundImage:imageWithColor(GJ_Btn_NormalColor) forState:UIControlStateNormal];
    [rightBtn setBackgroundImage:imageWithColor(GJ_Btn_HighlightedColor) forState:UIControlStateHighlighted];
    [rightBtn setBackgroundImage:imageWithColor(GJ_Btn_DisabledColor) forState:UIControlStateDisabled];
    
    self.rightView = rightBtn;
    self.rightViewMode = UITextFieldViewModeAlways;
    
    if (block) {
        __weak UIButton *button = rightBtn;
        [rightBtn addActionHandler:^(NSInteger tag) {
            block(button);
        }];
    }
    return rightBtn;
}

+(UITextField *)textfieldWithPlaceholder:(NSString *)placeholder
{
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, Screen_Width*2/3, kDefaultCellHeight)];
    textField.font = FontCustomSize(16);
    textField.textColor = Color_BlackFont;
    textField.returnKeyType = UIReturnKeyNext;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.textAlignment = NSTextAlignmentRight;
    textField.PlaceholderText = placeholder;
    return textField;
}

@end
